<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>用户管理-修改角色</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/layuimini/lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/layuimini/css/layuimini.css}" media="all">
    <link rel="stylesheet" th:href="@{/layuimini/css/public.css}" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div class="layui-form layuimini-form">
            <input name="userId" type="hidden" th:value="${sysUser?.userId}"/>
            <div class="layui-form-item">
                <label class="layui-form-label required">用户名</label>
                <div class="layui-input-block">
                    <input type="text" name="userName" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="请输入用户名" th:value="${sysUser?.username}" class="layui-input" disabled>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label required">用户角色</label>
                <div class="layui-input-block">
                    <div id="roleIds" name="roleIds" class="xm-select-demo"></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
                </div>
            </div>
        </div>
    </div>
</div>
<script th:src="@{/layuimini/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
<script th:src="@{/layuimini/js/lay-config.js?v=1.0.4}" charset="utf-8"></script>
<script type="text/javascript" th:inline="javascript">
    layui.use(['form','xmSelect','notice'], function () {
        let form = layui.form,
            layer = layui.layer,
            xmSelect = layui.xmSelect,
            notice = layui.notice,
            $ = layui.$;

        let ctxPath = /*[[@{/}]]*/'';

        renderRoleSelect();
        function renderRoleSelect() {
            $.ajax({
                type:'GET',
                url:ctxPath+'system/sysUserRole/userRoleTree?userId='+[[${sysUser?.userId}]],
                dataType:"json",
                success:function (res){
                    if(res.code === 200){
                        xmSelect.render({
                            el: '#roleIds',
                            name:'roleIds',
                            filterable: true,               // 开启搜索
                            radio: false,
                            data: res.data,
                            theme:{
                                color:'#1E9FFF'
                            },
                        })
                    }
                }
            });
        }

        //监听表单提交按钮
        form.on('submit(saveBtn)', function (data) {
            $.ajax({
                type: 'PUT',
                url: ctxPath+'system/sysUserRole/updateUserRoleByUserId',
                data:JSON.stringify(data.field),
                contentType: 'application/json;charset=utf-8;',
                dataType:'json',
                success:function (res){
                    if(res.code === 200){
                        notice.success('操作成功');
                        setTimeout(function() {
                            layer.closeAll("iframe");
                            parent.location.reload();
                        },1000);
                    }else{
                        notice.error(res.msg);
                    }
                }
            });
            return false;
        });
    });
</script>
</body>
</html>